import js from '@eslint/js' // js 推荐配置
import importSort from 'eslint-plugin-simple-import-sort' // 用来做导入导出排序
import pluginVue from 'eslint-plugin-vue' // eslint vue 支持
import globals from 'globals' // 指定一些 eslint 不知的全局变量
import vueEslintParser from 'vue-eslint-parser' // vue 解析器
import tsParser from '@typescript-eslint/parser' // ts 解析器
import tseslint from 'typescript-eslint' // ts 规则集合
import pluginPrettier from 'eslint-plugin-prettier' // 用来做代码格式化

// 忽略目录
const ignores = [
  'dist',
  'build',
  '**/*.js',
  '**/*.mjs',
  '**/*.d.ts',
  'eslint.config.js',
  'commitlint.config.js'
]

const common = {
  rules: {
    'prettier/prettier': 'error'
  },
  plugins: {
    prettier: pluginPrettier
  }
}

const frontendConfig = {
  files: ['**/*.{ts,tsx,vue}' /* '*.js' */],
  ignores: [],
  rules: {
    ...js.configs.recommended.rules,
    ...pluginVue.configs['flat/recommended'].rules,
    'no-unused-vars': 'error',
    'no-undef': 'warn',
    'no-console': 'error',
    'simple-import-sort/imports': 'error',
    'simple-import-sort/exports': 'error',
    'vue/valid-define-emits': 'error',
    '@typescript-eslint/no-explicit-any': 'off',
    'no-unused-vars': 'off'
  },
  languageOptions: {
    globals: {
      ...globals.browser,
      computed: 'readonly',
      defineEmits: 'readonly',
      defineExpose: 'readonly',
      defineProps: 'readonly',
      onMounted: 'readonly',
      onUnmounted: 'readonly',
      reactive: 'readonly',
      ref: 'readonly',
      shallowReactive: 'readonly',
      shallowRef: 'readonly',
      toRef: 'readonly',
      toRefs: 'readonly',
      watch: 'readonly',
      watchEffect: 'readonly'
    },
    parser: vueEslintParser,
    parserOptions: {
      extraFileExtensions: ['.vue'],
      // ecmaVersion: 'latest',
      ecmaFeatures: {
        jsx: true
      },
      parser: tsParser,
      sourceType: 'module'
      // project: [
      //     path.join(import.meta.dirname, 'tsconfig.eslint.json'),
      //     path.join(import.meta.dirname, '../../**/*/tsconfig.json')
      // ],
      // // project: path.join(import.meta.dirname, 'tsconfig.eslint.json'),
      // tsconfigRootDir: import.meta.dirname
    }
  },
  plugins: { vue: pluginVue, 'simple-import-sort': importSort }
}

const backendConfig = {
  files: ['apps/backend/**/*.ts'],
  extends: [tseslint.configs.recommended],
  languageOptions: {
    globals: {
      ...globals.node
    },
    parser: tseslint.parser
  },
  rules: {
    '@typescript-eslint/explicit-function-return-type': 'off',
    '@typescript-eslint/explicit-module-boundary-types': 'off',
    '@typescript-eslint/interface-name-prefix': 'off',
    '@typescript-eslint/no-explicit-any': 'off',
    'no-unused-vars': 'off',
    '@typescript-eslint/no-unused-vars': 'error',
    'no-console': 'error'
  }
}

export default tseslint.config(common, frontendConfig, backendConfig)
